﻿//********************************************************************************//
//创建日期：<创建日期，2016-10-30>
//创建作者：<郭登杰>
//功能说明：来函加价信息列表
//********************************************************************************//
using BLL.Access;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using Entity;
using BLL;
using DevExpress.Web;

namespace Web.Controls
{
    public partial class UCLHJIAJIAXXListQS : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        /// 来函流程步骤
        /// </summary>
        public eLaiHanLCBH LaiHanLC
        {
            get
            {
                return ViewState["LaiHanLC"] == null ? eLaiHanLCBH.未登记 : (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), Convert.ToString(ViewState["LaiHanLC"]));
            }
            set
            {
                ViewState["LaiHanLC"] = value.ToString();
            }
        }

        /// <summary>
        /// 设置复选框列显隐
        /// </summary>
        public bool ShowCheck;

        /// <summary>
        /// 记录来函编号
        /// </summary>
        public string LaiHanBH
        {
            get
            {
                return Convert.ToString(ViewState["LaiHanBH"]);
            }
            set
            {
                ViewState["LaiHanBH"] = value;
            }
        }

        /// <summary>
        /// 记录井原用户号
        /// </summary>
        public string YuanYongHuH
        {
            get
            {
                return Convert.ToString(ViewState["YuanYongHuH"]);
            }
            set
            {
                ViewState["YuanYongHuH"] = value;
            }
        }
        /// <summary>
        /// 记录目标用户号
        /// </summary>
        public string TargetYongHuH
        {
            get
            {
                return Convert.ToString(ViewState["TargetYongHuH"]);
            }
            set
            {
                ViewState["TargetYongHuH"] = value;
            }
        }

        /// <summary>
        /// 设置列表高度
        /// </summary>
        public int ListHeight
        {
            set
            {
                this.gvList.Settings.VerticalScrollableHeight = value;
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 移除操作
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            string s_yonghuh = e.CommandArgument.ToString();
            //清除结果集中对应记录
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                if (this.dtSource.Rows[i]["S_YONGHUH"].ToString() == s_yonghuh)
                {
                    this.dtSource.Rows.RemoveAt(i);
                }
            }
            this.BindData();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {

        }

        /// <summary>
        ///重新绑定
        /// </summary>
        public void BindData()
        {
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        public void ClearInfo()
        {
            this.dtSource = null;
            this.gvList.DataSource = null;
            this.gvList.DataBind();
        }

        public void AddItem(QS_LH_JIAJIATZXX item)
        {
            if (this.dtSource == null)
            {
                this.dtSource = new DataTable();
                this.dtSource.Columns.Add("S_YONGHUH");
                this.dtSource.Columns.Add("S_YONGHUM");
                this.dtSource.Columns.Add("I_Y");
                this.dtSource.Columns.Add("I_KCJHYUAN");
                this.dtSource.Columns.Add("I_KCJHXIN");
                this.dtSource.Columns.Add("I_HGJHYUAN");
                this.dtSource.Columns.Add("I_KCLSJ");
                this.dtSource.Columns.Add("I_HGLSJ");
                this.dtSource.Columns.Add("I_CJHL");
                this.dtSource.Columns.Add("I_JJL");
                this.dtSource.Columns.Add("I_TZL");
            }
            //判断是否已添加了该项
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                if(this.dtSource.Rows[i]["S_YONGHUH"].ToString()==item.S_YONGHUH)
                {
                    return;
                }
            }
            DataRow dr = this.dtSource.NewRow();
            dr["S_YONGHUH"] = item.S_YONGHUH;
            dr["S_YONGHUM"] = item.S_YONGHUM;
            dr["I_Y"] = item.I_Y;
            dr["I_KCJHYUAN"] = item.I_KCJHYUAN;
            dr["I_KCJHXIN"] = item.I_KCJHXIN;
            dr["I_HGJHYUAN"] = item.I_HGJHYUAN;
            dr["I_KCLSJ"] = item.I_KCLSJ;
            dr["I_HGLSJ"] = item.I_HGLSJ;
            dr["I_CJHL"] = item.I_CJHL;
            dr["I_JJL"] = item.I_JJL;
            dr["I_TZL"] = item.I_TZL;
            this.dtSource.Rows.Add(dr);
            this.BindData();
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <returns></returns>
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();
            if (this.dtSource == null || this.dtSource.Rows.Count == 0)
            {
                rs.Msg = "请添加调整信息。";
                return rs;
            }
            string y="";
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                if(this.dtSource.Rows[i]["I_Y"].ToString()!=y)
                {
                    if (y == "")
                    {
                        y = this.dtSource.Rows[i]["I_Y"].ToString();
                    }
                    else
                    {
                        rs.Msg = "加价调整信息中水量年不一致。";
                        return rs;
                    }
                }
            }
            rs.IsSuccess = true;
            return rs;
        }

      
        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
                this.LaiHanBH = laihanxx.S_LAIHANBH;
            }
            if (laihanxx.S_LAIHANBH != null)
            {
                this.dtSource = SearchSpHelper.P_QS_JJSLXXHQ(laihanxx.S_LAIHANBH, null, null).Tables[0];
                this.BindData();
                if((int)laihanxx.I_LIUCHENGBH>=(int)eLaiHanLCBH.登记)
                {
                    this.gvList.Columns[11].Visible = false;
                }
            }
        }

        /// <summary>
        /// 获取申请信息
        /// </summary>
        /// <returns></returns>
        public List<QS_LH_JIAJIATZXX> GetShengQingXX()
        {
            List<QS_LH_JIAJIATZXX> list = new List<QS_LH_JIAJIATZXX>();
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                QS_LH_JIAJIATZXX obj = new QS_LH_JIAJIATZXX();
                object[] vals = (object[])gvList.GetRowValues(i, "S_YONGHUH", "S_YONGHUM", "I_Y", "I_KCJHYUAN", "I_KCJHXIN", "I_HGJHYUAN", "I_KCLSJ", "I_HGLSJ", "I_CJHL", "I_JJL", "I_TZL");
                obj.S_YONGHUH = vals[0].ToString();
                obj.S_YONGHUM = vals[1].ToString();
                obj.I_Y = Convert.ToInt32(vals[2]);
                obj.I_KCJHYUAN = Convert.ToInt32(vals[3]);
                obj.I_KCJHXIN = Convert.ToInt32(vals[4]);
                obj.I_HGJHYUAN = Convert.ToInt32(vals[5]);
                obj.I_KCLSJ = Convert.ToInt32(vals[6]);
                obj.I_HGLSJ = Convert.ToInt32(vals[7]);
                obj.I_CJHL = Convert.ToInt32(vals[8]);
                obj.I_JJL = Convert.ToInt32(vals[9]);
                obj.I_TZL = Convert.ToInt32(vals[10]);
                list.Add(obj);
            }
            return list;
        }

        /// <summary>
        /// 添加申请信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void CreateShengQingXX(YS_LH_LAIHANXX laihanxx)
        {
            //清除记录
            QS_LH_JIAJIATZXXProcess bllT2 = new QS_LH_JIAJIATZXXProcess();
            bllT2.DeleteQS_LH_JIAJIATZXXesDynamic("S_LAIHANBH='" + laihanxx.S_LAIHANBH+"'");
            //添加记录
            List<QS_LH_JIAJIATZXX> list = this.GetShengQingXX();
            foreach (QS_LH_JIAJIATZXX item in list)
            {
                item.S_LAIHANBH = laihanxx.S_LAIHANBH;
                bllT2.InsertOrUpdate(item);
            }
        }

        #endregion
    }
}